<template>
  <div>
    <el-dialog :title="title" :visible="dialogFormVisible" width="75%" @close="handleClose">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label-width="15%" label="区域名称:" prop="name">
          <el-input v-model="form.name" maxlength="15" show-word-limit />
        </el-form-item>
        <el-form-item label-width="15%" label="备注说明:" prop="remark">
          <el-input v-model="form.remark" type="textarea" maxlength="30" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addPointList, editStreet } from '@/api/area'
export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
      required: true,
      default: false,
      loading: false
    }
  },
  data() {
    return {
      form: {
        name: '', remark: ''
      },
      rules: { name: [{ required: true, message: '角色名不得为空', trigger: 'blur' }],
        remark: [{ required: true, message: '角色名不得为空', trigger: 'blur' }]
      },
      loading: false
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑区域' : '新增区域'
    },

    submit() {
      return this.form.id ? this.editstreet : this.addstreet
    }
  }, created() {
    console.log(this.form)
  },
  methods: {
    handleClose() {
      this.$emit('update:dialogFormVisible', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', remark: ''
      }
    },
    async addstreet() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        await addPointList({ regionName: this.form.name, remark: this.form.remark })
        console.log(this.$parent.getList(1))
        this.$message.success('新增成功')
        this.handleClose()
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    async editstreet() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        await editStreet(this.form.id, { regionName: this.form.name, remark: this.form.remark })
        this.$message.success('编辑成功')
        this.$parent.getList(this.$parent.newdata.pageIndex)
        this.handleClose()
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    }
  }

}
</script>

<style>

</style>
